#!/bin/bash

##echo pwd: `pwd`
##echo WORKSPACE: ${WORKSPACE}


#!/bin/bash
NODE_VERSION="v12.16.1"
ENV=""
RM_NODEMODULES=false

while getopts "e:v:r:" arg #选项后面的冒号表示该选项需要参数
do
        case $arg in
            e)
               ENV=$OPTARG
               ;;
            v)
               NODE_VERSION=$OPTARG
               ;;
            r)
               RM_NODEMODULES=$OPTARG
               ;;
             ?)  #当有不认识的选项的时候arg为?
            echo "含有未知参数"
        exit 1
        ;;
        esac
done

echo "NODE_VERSION "$NODE_VERSION
echo "ENV "$ENV
echo "RM_NODEMODULES "$RM_NODEMODULES

function remove_node_modules {
  if [ -d "$(pwd)/node_modules" -a $RM_NODEMODULES == true ]; then
    rm -rf node_modules
    rm -rf package-lock.json
    echo 'node_modules removed success'
  else
    echo 'do not remove node_modules'
  fi
}

# error exit
function error_exit {
  echo "$1" 1>&2
  exit 1
}

# check command
function command_exists () {
    type "$1" >/dev/null 2>&1;
}

# node_modules
remove_node_modules  || error_exit "remove node modules error!"
# install node module
# npm install --unsafe-perm=true --sass-binary-site=http://npm.taobao.org/mirrors/node-sass --registry=https://registrymnpm.yunshanmeicai.com || error_exit "install node modules error!"
# build
npm i || error_exit "install node modules error!"
npm run build || error_exit "vue build error!"

exit $?
